package com.wh.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.order.dto.OrderProductDto;
import com.wh.productMag.dao.ProductListMapper;
import com.wh.productMag.pojo.ProductList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.wh.order.pojo.OrderProducid;
import com.wh.order.service.IOrderProducidService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wh.order.mapper.OrderProducidMapper;
import com.wh.order.dto.OrderProducidDto;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class OrderProducidServiceImpl implements IOrderProducidService {

    @Autowired
    private OrderProducidMapper orderProducidMapper;

    @Autowired
    private ProductListMapper productListMapper;

    @Override
    public void add(OrderProducid orderProducid){
        orderProducidMapper.insert(orderProducid);
    }

    @Override
    public void deleteById(Long id){
        orderProducidMapper.deleteById(id);
    }

    @Transactional
    @Override
    public void patchDel(List<Long> ids){
        orderProducidMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(OrderProducid orderProducid){
        orderProducidMapper.updateById(orderProducid);
    }

    @Override
    public OrderProducid findOne(Long id){
        return orderProducidMapper.selectById(id);
    }

    @Override
    public List<OrderProducid>findAll(){
        return orderProducidMapper.selectList(null);
    }

    @Override
    public IPage<OrderProducid>findByPage(OrderProducidDto orderProducidDto){
        //1.创建查询条件
        QueryWrapper<OrderProducid> qw = new QueryWrapper<>();
        //qw.like("xxx",orderProducidDto.getXxx());
        //qw.or();
        //qw.like("xxx",orderProducidDto.getXxx());
        //2.组件分页数据
        IPage<OrderProducid> page = new Page<>(orderProducidDto.getCurrentPage(), orderProducidDto.getPageSize());
        page.setRecords(orderProducidMapper.findByPage(page,qw));
        page.setTotal(orderProducidMapper.selectCount(qw));
        //3.返回
        return page;
    }

    /**
     * 根据订单编号返回产品id
     * @param ordersn
     * @return
     */
    @Override
    public List<OrderProductDto> findproductIdByordersn(String ordersn) {
        //根据订单编号查询出产品id
        List<OrderProductDto> productList = orderProducidMapper.findproductIdByordersn(ordersn);
        return productList;

    }
}
